<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymleaf.org">
<head>
    <meta charset="UTF-8">
    <link th:href="@{/css/bootstrap.min.css}" type="text/css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <script th:src="@{/js/jquery.md5.js}" type="text/JavaScript"></script>
    <script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery.validate.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/layer/layer.js}"></script>
    <title>登录</title>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-4 column">
        </div>
        <div class="col-md-4 column">
            <h3 class="text-center">
                登录
            </h3>
        </div>
        <div class="col-md-4 column">
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-8 column">
        </div>
        <div class="col-md-4 column">
            <form class="form-horizontal" role="form" id="loginForm">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">手机</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="inputEmail3" name = "inputEmail3"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
                    <div class="col-sm-10">
                        <input type="password" class="form-control" id="inputPassword3" name="inputPassword3"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <div class="checkbox">
                            <label><input type="checkbox" />Remember me</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default" onclick="login()">登录</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<style>
    .error{
        color:red;
        font-weight: normal;
    }
</style>
<script>
    //增加手机号验证规则
    $.validator.addMethod("isMobile", function(value, element) {
        var length = value.length;
        var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写您的手机号码！");
    $.extend($.validator.messages,{
        required:"该字段不能为空！",
    });
    var g_password_salt = "1a2b3c4d";
    function login() {
        $("#loginForm").validate({
            rules:{
                inputEmail3:{
                    isMobile:true,
                    required:true,
                },
                inputPassword3:{
                    required:true,
                }
            },
            submitHandler : function (form) {
                doLogin();
            }
        })
    }
    function doLogin() {
        g_showLoading();
        var inputPass = $("#inputPassword3").val();
        var salt = g_password_salt;
        var str = salt.charAt(0) + inputPass + salt.charAt(5) + salt.charAt(4) + salt.charAt(1);
        var password = $.md5(encodeURIComponent(str));
        console.log(inputPass);
        $.ajax({
            url : "/login/doLogin",
            type : "POST",
            data : {
                mobile : $("#inputEmail3").val(),
                password : password
            },
            success: function (data) {
                layer.closeAll();
                if(data.code == 200){
                    layer.msg("成功");
                    window.location.href = "/goods/toList";
                }else {
                    layer.msg(data.message);
                }
            },
        })
    }
    function g_showLoading() {
        return layer.msg('处理中...', {
            icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '0px',
            time: 100000
        });
    }
</script>
</html>